/* eslint-disable comma-dangle */
/* eslint-disable prefer-arrow-callback */
import axios from "axios";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { isLogin } from "@/utils/tools";
// 创建一个axios实例
const instance = axios.create({
  timeout: 5000, // 请求超时时间
  baseURL: "http://192.168.15.26:1337", // 网络请求的基础地址
  // baseURL: "http://localhost:1337",
});

// https://github.com/axios/axios
// 全局拦截器
// Add a request interceptor
//  全局请求拦截，发起网络请求之前执行
instance.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    NProgress.start(); // 启动进度条
    config.headers.token = isLogin();
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
// 全局响应拦截，网络请求完成之后执行
instance.interceptors.response.use(
  function (response) {
    NProgress.done(); // 关闭进度条
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response.data;
  },
  function (error) {
    NProgress.done(); // 关闭
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  }
);

/**
 * 发起get请求
 * @param {*} url
 * @param {*} params
 * @returns
 */
export const get = (url, params) => instance.get(url, { params });

/**
 * 发起post请求
 * @param {*} url
 * @param {*} data
 * @returns
 */
export const post = (url, data) => instance.post(url, data);

/**
 * put 请求
 *
 */

export const put = (url, data) => instance.put(url, data);

/**
 * delete请求
 */
export const del = (url) => instance.delete(url);
